import 'package:flutter/material.dart';

void main() {
  runApp(const _MyApp());
}

class _MyApp extends StatelessWidget {
  const _MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: "Flutter Demo",
        theme: ThemeData(
            colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
            useMaterial3: true),
        home: const _MyHomePage());
  }
}

class _MyHomePage extends StatelessWidget {
  const _MyHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("InheritedWidget Demo"),
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      ),
      body: const Text("Test"),
    );
  }
}

/* 参考

class Item {
  String reference = "";
  Item(this.reference);
}

class _MyInherited extends InheritedWidget {
  const _MyInherited({Key key, required Widget child, required this.data})
      : super(key: key, child: child);

  final MyInheritedWidgetState data;

  @override
  bool updateShouldNotify(_MyInherited oldWidget) {
    return true;
  }
}

class MyInheritedWidget extends StatefulWidget {
  const MyInheritedWidget({Key key, this.child}) : super(key: key);

  final Widget child;

  @override
  MyInheritedWidgetState createState() => MyInheritedWidgetState();

  static MyInheritedWidgetState of([BuildContext context, bool rebuild = true]){
    return (
      rebuild ? context.inheritFromWidgetOfExactType(_MyInherited) as _MyInherited
      : context.ancestorWidgetOfExactType(_MyInherited) as _MyInherited).data;
  }

}

class MyInheritedWidgetState extends State<MyInheritedWidget> {
  final List<Item> _items = <Item>[];

  int get itemsCount => _items.length;

  void addItem(String reference) {
    setState(() {
      _items.add(Item(reference));
    });
  }

  @override
  Widget build(BuildContext context) {
    return _MyInherited(
      data: this,
      child: widget.child,
    );
  }
}

class MyTree extends StatefulWidget {
  const MyTree({super.key});

  @override
  State<StatefulWidget> createState() => _MyTreeState();
}

class _MyTreeState extends State<MyTree> {
  @override
  Widget build(BuildContext context) {
    return MyInheritedWidget(
      child: Scaffold(
        appBar: AppBar(
          title: const Text("InheritedWidget Demo"),
        ),
        body: const Column(
          children: <Widget>[
            WidgetA(),
            Row(
              children: <Widget>[
                Icon(Icons.shopping_cart),
                WidgetB(),
                WidgetC()
              ],
            )
          ],
        ),
      )
    );
  }
}

class WidgetA extends StatelessWidget {
  const WidgetA({super.key});

  @override
  Widget build(BuildContext context) {
    final MyInheritedWidgetState state = MyInheritedWidget.of(context, false);
    return Container(
      child: RaisedButton()(
        child: const Text("Add Item"),
        onPressed: () {
          state.addItem("new item");
        }
      ),
    );
  }
}

class WidgetB extends StatelessWidget {
  const WidgetB({super.key});

  @override
  Widget build(BuildContext context) {
    final MyInheritedWidgetState state = MyInheritedWidget.of(context);

    return Text("${state.itemsCount}");
  }
}

class WidgetC extends StatelessWidget {
  const WidgetC({super.key});

  @override
  Widget build(BuildContext context) {
    return const Text("I am Widget C");
  }
}
*/